/*
Purpose: Tables and Figures for "Can Public School Rankings Improve School Performance? Evidence from a Nation-Wide Reform in Tanzania"
Author: Jacobus Cilliers
Last Updated: 20 January 2020
*/

********************************************************************************
*******************************Preliminaries************************************
********************************************************************************

	clear
	set more off
	set matsize 800
	
	*Note: enter your own root folder: 
	global root "C:\Users\jacob\Box Sync\RISE Tanzania_Research\Papers\BRN_LearningImpacts\JHR Replication dta and do files"

	global fig "$root/fig"
	global tables "$root/tables"
	global dta "$root/data"

	//Scheme for figures
	set scheme  plotplainblind			
		
/****************************************
*******Define Globals (for figure) **********			
****************************************/			

		use "$dta/admin_analysis_primary_newvars.dta", clear

		set emptycells drop
		set matsize 2000
		
		//Define controls (so easier to follow)
		global lagged_district_rank 		L_rankdist* // Dummy for each decile of within-district rank (lagged)
		global lagged_y 					Lnecta_avgmarks Lnecta_avgmarks2 Lnecta_avgmarks3 Lnecta_avgmarks4 //Quadratic controls for lagged test performance. 
		forvalues i = 2011/2106 {
				global lagged_y_`i'			y_`i'xLnecta_avgmarks* //Interacting the quadratic controls with each year, to allow for relationship between past and current performance to vary year-by-year. 
		}
		global lagged_y_rank 				L_ranknat*   //Dummy for each declie of national-level rank (lagged). 
		global lagged_post_x_district_rank 	postxL_rankdist* //Interacting the variables "$lagged_district_rank" with a dummy equal to one if it is the post-BRN period. 
		
		
		foreach i in 1 2 3 4 7 8 9 10 { //Labelling the rank variables. 
			lab var L_rankdist`i' "`i'"
			lab var postxL_rankdist`i' "`i'"
			}
				
		******************
		****Figure 1. ****
		******************
		
		**Panels B and D (First differences)
		eststo clear
			foreach var of varlist necta_avgmarks  necta_passrate {
				qui areg `var'  /// 
						$lagged_district_rank ///
						$lagged_y ///
					if year == 2012, absorb(district_x_year) cl(necta_districtname)
				eststo Pre
			
				qui areg `var'  /// 
						$lagged_district_rank ///
						y_*xLnecta_avgmarks* /// allows functional form of f(y_t-1) to vary from year to year					
					if post == 1, absorb(district_x_year) cl(necta_districtname)
				eststo Post
				coefplot Post Pre, keep(L_rankdist*) yline(0) vertical levels(90 /*68.2*/)
				gr export "$fig/first_difference_`var'.png", replace	
			}		
		** Panels A and C (Second difference)
			foreach var of varlist necta_avgmarks necta_passrate necta_passedcandidates necta_cleancandidates {
				qui areg `var' ///
						$lagged_district_rank ///
						$lagged_post_x_district_rank ///
						y_*xLnecta_avgmarks* /// allows functional form of f(y_t-1) to vary from year to year
						, absorb(district_x_year) cl(necta_districtname)
				eststo dd_`var'	
				coefplot , keep(postxL_rankdist*) yline(0) vertical  levels(90 /*68.2*/)
				gr export "$fig/dd_`var'.png", replace
			}

	
	
/***********************
*******Tables **********			
***********************/			
		use "$dta/admin_analysis_primary_newvars.dta", clear

		set emptycells drop
		set matsize 2000		
	
		lab var postxL_rankdist10 "90-100th percentile in previous year"
		lab var postxL_rankdist9 "80-90th percentile in previous year"		
		lab var postxL_rankdist2 "10-20th percentile in previous year"		
		lab var postxL_rankdist1 "0-10th percentile in previous year"		
		
		lab var L_rankdist10 "90-100th percentile in previous year"
		lab var L_rankdist9 "80-90th percentile in previous year"		
		lab var L_rankdist2 "10-20th percentile in previous year"		
		lab var L_rankdist1 "0-10th percentile in previous year"		

		
		*****************************************************
		** Table 1: Descriptives (create table manually)
		*****************************************************
		gen emis_std_1_to_5_total = emis_standard1_total + emis_standard2_total + emis_standard3_total + emis_standard4_total + emis_standard5_total
		foreach var of varlist 	necta_avgmarks necta_passrate  necta_passedcandidates ///
								emis_standard4_total emis_standard5_total emis_standard6_total emis_standard7_total ///
								emis_private2015 emis_private_classsizes2016 {
			tab year, sum(`var')
		}
		**************
		***Table 2 ***
		**************
		eststo clear
		*Columns (1), (3), and (5). 
		foreach var of varlist necta_avgmarks necta_passrate  necta_passedcandidates {
			qui areg `var' ///
				L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
				y_*xLnecta_avgmarks* /// 
				, absorb(district_x_year) cl(necta_districtname)

			qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1
			local m = `r(mean)'
			eststo dd`var', addscalars(m `m' )
			}
				
		*The F test of eqaulity of alpha coefficients. (mentioned in words in the paper). Basically a balance test. 
		qui areg necta_avgmarks ///
				L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
				y_*xLnecta_avgmarks* /// 
				, absorb(district_x_year) cl(necta_districtname)
		test L_rankdist10 = L_rankdist9 = L_rankdist1 =L_rankdist2 = 0
		
		*Columns (2), (4), and (6). 
		foreach var of varlist necta_avgmarks necta_passrate  necta_passedcandidates {
			qui reghdfe `var' ///
				L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
				y_*xLnecta_avgmarks* /// 
				, absorb(district_x_year rowid) cl(districtnum_forall)

			qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1
			local m = `r(mean)'
			eststo fe`var', addscalars(m `m' )
			}

		*Export Table 2. 
		esttab 	ddnecta_avgmarks fenecta_avgmarks ddnecta_passrate fenecta_passrate ddnecta_passedcandidates fenecta_passedcandidates ///
				using "$tables/deciles.tex", replace ///
				tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(m N r2,  fmt(2 0 3) labels("Control mean, post BRN" "Observations" "R-squared")) ///	
				keep(postxL_rankdist1 postxL_rankdist2) ///
				collabels(none)	 ///
				nomtitle ///
				mgroups("Marks" "Pass rate" "Number passed" , pattern(1 0 1 0 1 0) ///
				prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
				indicate("Diff-diff=L_rankdist10" "School fixed effects= y_2016xLnecta_avgmarks3" "Control lagged exam score=y_2016xLnecta_avgmarks2") /// Note: I have to manually change "fixed effects" indication.
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 

	*Estimate averages in the bottom quintile (for counterfatual estimates):
	sum necta_avgmarks necta_passrate  necta_passedcandidates necta_cleancandidates if post == 1 & L_rankdist1 == 1
	gen rate = necta_passedcandidates / necta_cleancandidates
	count if rate ~= necta_passrate
	sum necta_avgmarks necta_passrate rate necta_passedcandidates necta_cleancandidates if post == 1 & L_rankdist1 == 1
		
		***************
		*** Table 3 ***
		***************
		*Column (1)
		qui reghdfe necta_cleancandidates ///
			L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_201*xLnecta_avgmarks* , absorb(district_x_year rowid) cl(districtnum_forall)
		qui sum necta_cleancandidates if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1
		local m = `r(mean)'
		eststo fe_cleancandidates, addscalars(m `m')
		*Column (2)
		qui reghdfe necta_cleancandidates ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_2016xLnecta_avgmarks* y_2015xLnecta_avgmarks* if (year == 2015 | year == 2016), absorb(district_x_year rowid) cl(districtnum_forall)
		qui sum necta_cleancandidates if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & (year == 2015 | year == 2016)
		local m = `r(mean)'
		eststo fe_cleancandidates_15_16, addscalars(m `m')
		*Column (3) to (5)
		foreach var of varlist enroll_4_to_6 emis_standard6_total emis_standard7_total {
			qui reghdfe `var' ///
				postxL_rankdist10 postxL_rankdist9 postxL_rankdist2 postxL_rankdist1 ///
				y_*xLnecta_avgmarks* if (year == 2015 | year == 2016), absorb(district_x_year rowid) cl(districtnum_forall)
			qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & (year == 2015 | year == 2016)
			local m = `r(mean)'
			eststo fe_`var', addscalars(m `m')
				}
		*Column (6). 
		qui areg emis_7_to_6 ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_2016xLnecta_avgmarks* 	///
			, absorb(district_x_year) cl(necta_districtname)
			qui sum emis_7_to_6 if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & year == 2016
			local m = `r(mean)'			
			eststo ratio_7_to_6, addscalars(m `m')
		*Export Table 3. 
		esttab fe_cleancandidates fe_cleancandidates_15_16 fe_enroll_4_to_6 fe_emis_standard6_total fe_emis_standard7_total ratio_7_to_6  ///
			using "$tables/candidates_fe.tex", replace ///
			tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
			keep(postxL_rankdist1 postxL_rankdist2) ///
			stats(m N r2,  fmt(2 0 3) labels("Control mean" "Observations" "R-squared")) ///	
			mgroups("PSLE data--- exam sitters" "EMIS data---enrollment" , pattern(1 0 1 0 0 0 0) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			collabels(none)	 ///
			mtitles("All years" "2015 and 2016" "Std 4-6" "Std 6" "Std 7" "Std7/Std6") ///
			indicate("Diff-diff=L_rankdist10" "Control lagged exam score=y_2016xLnecta_avgmarks2" "Fixed effects = y_2016xLnecta_avgmarks3") ///
			nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 	
		*Note: I have to manually indicate that final regression is not fixed effects. 
		
		**Calculate means in bottom quintile (discussed in text of paper)
		estpost sum necta_avgmarks necta_passrate  necta_passedcandidates  ///
			necta_cleancandidates enroll_4_to_6 emis_standard6_total emis_standard7_total emis_7_to_6 ///
				if  L_rankdist1== 1 & post == 1		
			esttab using "$tables/means_bottom_decile.csv", replace ///
					cells("count(fmt(0)) mean(fmt(2))") label csv nonum gaps f noobs

					
	**Table 4. We do not have permission to share this data. 
					

		***************
		*** Table 5 ***
		***************

	eststo clear
	*Columns (1) and (2)
	qui areg necta_avgmarks ///
		L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		y_*xLnecta_avgmarks* /// 
		if Lbad == 0, absorb(district_x_year) cl(necta_districtname)
	eststo Above
	qui areg necta_avgmarks ///
		L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		y_*xLnecta_avgmarks* /// 
		if Lbad == 1, absorb(district_x_year) cl(necta_districtname)
	eststo Below
	*Columns (5) and (6)
	qui reghdfe necta_avgmarks ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		y_*xLnecta_avgmarks* /// 
		if Ldrop_pctile_30 == 1 & post, absorb(district_x_year rowid) cl(districtnum_forall)
	eststo Shock
	qui reghdfe necta_avgmarks ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		y_*xLnecta_avgmarks* /// 
		if Ldrop_pctile_30 == 0 & post, absorb(district_x_year rowid) cl(districtnum_forall)
	eststo None
	*Columns (7) and (8)
	qui areg necta_avgmarks ///
		L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		y_*xLnecta_avgmarks* /// 
		if school_size2012 == 1, absorb(district_x_year) cl(necta_districtname)
	eststo Smallest
	qui areg necta_avgmarks ///
		L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		y_*xLnecta_avgmarks* /// 
		if school_size2012 ~= 1, absorb(district_x_year) cl(necta_districtname)
	eststo Large
	*Columns (3) and (4) (Merge in the data indicating which districts STEP was implemented in)
	merge m:1 necta_districtname using "$dta\Step_districts.dta"			
	qui  areg necta_avgmarks ///
				L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
				y_*xLnecta_avgmarks* /// 
				if step_d == 1, absorb(district_x_year) cl(necta_districtname)
			qui sum necta_avgmarks if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1 & step_d == 1
			local m = `r(mean)'
			eststo STEP, addscalars(m `m' )		
	qui  areg necta_avgmarks ///
				L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///				
				y_*xLnecta_avgmarks* /// 
				if step_d ~= 1, absorb(district_x_year) cl(necta_districtname)
			qui sum necta_avgmarks if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1 & step_d ~= 1
			local m = `r(mean)'
			eststo Other, addscalars(m `m' )		
	*Output tables. 
	esttab Below Above  STEP Other Shock None  Smallest Large   ///
		using "$tables/robustness.tex", replace ///
		tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
		keep(postxL_rankdist1 postxL_rankdist2) ///
		collabels(none)	 ///
		mgroups("District performance" "STEP program" "Negative shock"  "Small school" , pattern(1 0 1 0 1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		indicate("Diff-diff=L_rankdist10" "Fixed effects = y_2016xLnecta_avgmarks3") /// Only "schock-none" includes school-level fixed effects.I need to make that adjustment manually. 
		nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 	

		
/*************************************
*******Appendix tables **********			
*************************************/		

	*****************
	*** Figure B1 *** 
	*****************

	foreach yr in "2011" "2012" "2013" "2014" "2015" "2016"  {
			tw sc prim_distrank_v1 necta_avgmarks if year == `yr', m(oh) ///
				ytitle("Within district percentile rank, `yr'") ///
				xtitle("Average exam score, 0-250 scale, `yr'") 

			graph export "$fig/districtranks_primary`yr'.png", replace
		}

	****************
	*** Table B1 *** No analysis conducted
	****************
		
	****************
	*** Table B2 *** 
	****************
	sum necta_passedcandidates necta_avgmarks   necta_cleancandidates necta_passrate if year == 2012 & _rankdist1
	sum necta_passedcandidates necta_avgmarks   necta_cleancandidates necta_passrate if year == 2012 & _rankdist2
		
	****************
	*** Table B3 ***
	****************
		
	*Columns (1) and (3)	
	gen bottom_20 = _rankdist1 | _rankdist2 	
	foreach var of varlist 	_rankdist1 bottom_20 {
	qui areg `var' ///
		L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_*xLnecta_avgmarks* /// 
			, absorb(district_x_year) cl(necta_districtname)
			qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1
			local m = `r(mean)'
			eststo dd`var', addscalars(m `m' )			
	}
	*Columns (2) and (4)	
	foreach var of varlist 	_rankdist1 bottom_20 {
			qui reghdfe `var' ///
				L_rankdist10 L_rankdist9 L_rankdist2 L_rankdist1 ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
				y_*xLnecta_avgmarks* /// 
				, absorb(district_x_year rowid) cl(districtnum_forall)

			qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & post == 1
			local m = `r(mean)'
			eststo fe`var', addscalars(m `m' )
	}
	*Export table
	esttab 	dd_rankdist1 fe_rankdist1 ddbottom_20 febottom_20 ///
			using "$tables/deciles_outcome_distrank.tex", replace ///
			tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
			stats(N r2,  fmt(0 3) labels("Observations" "R-squared")) ///	
			keep(postxL_rankdist1 postxL_rankdist2) ///
			collabels(none)	 ///
			nomtitle ///
			mgroups("Bottom decile" "Bottom quintile", pattern(1 0 1 0) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			indicate("Diff-diff=L_rankdist10" "School fixed effects= y_2016xLnecta_avgmarks3" "Control lagged exam score=y_2016xLnecta_avgmarks2") /// Note: I have to manually change "fixed effects" indication.
			nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 

	****************
	*** Table B4 ***
	****************
	*Testing for sorting. 
	*Label Ward-level average ranks. 
		lab var postxLward_mean_rankdist10 "Ward: 90-100th pctile in previous year"
		lab var postxLward_mean_rankdist9 "Ward: 80-90th pctile in previous year"		
		lab var postxLward_mean_rankdist2 "Ward: 10-20th pctile in previous year"		
		lab var postxLward_mean_rankdist1 "Ward: 0-10th pctile in previous year"		
		
		lab var Lward_mean_rankdist10 "90-100th percentile in previous year"
		lab var Lward_mean_rankdist9 "80-90th percentile in previous year"		
		lab var Lward_mean_rankdist2 "10-20th percentile in previous year"		
		lab var Lward_mean_rankdist1 "0-10th percentile in previous year"		
	*Regression
	qui reghdfe necta_cleancandidates ///
		L_rankdist1 L_rankdist2 L_rankdist10 L_rankdist9   ///
		postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
		Lward_mean_rankdist1 Lward_mean_rankdist2  Lward_mean_rankdist9  Lward_mean_rankdist10 ///
		postxLward_mean_rankdist1 postxLward_mean_rankdist2  postxLward_mean_rankdist9  postxLward_mean_rankdist10 ///
		y_201*xLnecta_avgmarks* , absorb(district_x_year rowid) cl(districtnum_forall)
	eststo cleancandidates_wardmean	
	*F test of "pure spillovers"
	test 	(postxLward_mean_rankdist10 = postxL_rankdist10) (postxLward_mean_rankdist9 = postxL_rankdist9) ///
			(postxLward_mean_rankdist1 = postxL_rankdist1) (postxLward_mean_rankdist2 = postxL_rankdist2) 
	*Export table
	esttab cleancandidates_wardmean using "$tables/spillovers.tex", replace ///
		tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
		keep(postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10 postxLward_mean_rankdist1 postxLward_mean_rankdist2 postxLward_mean_rankdist9 postxLward_mean_rankdist10 ) ///
		collabels(none)	 ///
		mtitles("Exam sitters") ///
		nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 		
	
	****************
	*** Table B5 ***
	****************
		foreach var of varlist necta_avgmarks necta_passrate  necta_passedcandidates {
			qui reghdfe `var' ///
					postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
					y_2016xLnecta_avgmarks* y_2015xLnecta_avgmarks* y_2014xLnecta_avgmarks* y_2013xLnecta_avgmarks*  ///
					[pweight=sdi_weight] if  has_sdi_anyyear & year > 2012, absorb(district_x_year rowid) cl(districtnum_forall)			
				qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & year > 2012 & has_sdi_anyyear == 1
				local m = `r(mean)'
				eststo `var'_all, addscalars(m `m')
			}
		esttab 	 necta_avgmarks_all necta_passrate_all necta_passedcandidates_all ///
				using "$tables/main_results_sdi_sample.tex", replace ///
				tex label se(2) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
				stats(m N r2,  fmt(2 0 3) labels("Control mean, post BRN" "Observations" "R-squared")) ///	
				keep( postxL_rankdist1 postxL_rankdist2 ) ///
				mtitles("Marks" "Pass rate" "Number passed") ///
				nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 
			
	
	**B.6. SDI data We do not have permission to share this. 
	

	****************
	*** Table B7 ***
	****************
	**Impacts on enrollment, by gender. 
		foreach var of varlist enroll_4_to_6_male enroll_4_to_6_female emis_standard7_male emis_standard7_female {
			qui reghdfe `var' ///
				postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
				y_*xLnecta_avgmarks* if (year == 2015 | year == 2016), absorb(district_x_year rowid) cl(districtnum_forall)
			qui sum `var' if L_rankdist10==0 & L_rankdist9==0 & L_rankdist2==0 & L_rankdist1 ==0 & (year == 2015 | year == 2016)
			local m = `r(mean)'
			eststo `var', addscalars(m `m')
				}		
		esttab  enroll_4_to_6_male enroll_4_to_6_female emis_standard7_male emis_standard7_female  ///
			using "$tables/enrollment_gender.tex", replace ///
			tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
			keep(postxL_rankdist1 postxL_rankdist2) ///
			stats(m N r2,  fmt(2 0 3) labels("Control mean" "Observations" "R-squared")) ///	
			mgroups("Grades 4-6" "Grade 7" , pattern(1 0 1 0) ///
			prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
			collabels(none)	 ///
			mtitles("Male" "Female" "Male" "Female") ///
			nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 				
	
	****************
	*** Table B8 ***
	****************
	foreach num in 10 15 20 25 30 {
		qui reghdfe necta_avgmarks ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_*xLnecta_avgmarks* /// 
			if Ldrop_pctile_`num' == 1 & post, absorb(district_x_year rowid) cl(districtnum_forall)
		eststo Shock_`num'
		qui reghdfe necta_avgmarks ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_*xLnecta_avgmarks* /// 
			if Ldrop_pctile_`num' == 0 & post, absorb(district_x_year rowid) cl(districtnum_forall)
		eststo None_`num'
	}
	
	esttab Shock_10 None_15 Shock_20 None_20 Shock_25 None_25 Shock_30 None_30   ///
		using "$tables/robustness_shocks.tex", replace ///
		tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
		keep(postxL_rankdist1 postxL_rankdist2 ) ///
		collabels(none)	 ///
		mgroups("10 pctile drop" "15 pctile drop" "20 pctile drop" "25 pctile drop"  "30 pctile drop" , pattern(1 0 1 0 1 0 1 0 1 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 	
//		indicate("Diff-diff=L_rankdist10" "Fixed effects = y_2016xLnecta_avgmarks3") /// Only "schock-none" includes individual level fixed effects.I need to make that adjustment manually. 
	
		
	****************
	*** Table B9 ***
	****************
	foreach var of varlist necta_avgmarks necta_passrate {
		qui areg `var' ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_*xLnecta_avgmarks* /// 
			if post == 1, absorb(district_x_year) cl(necta_districtname)
		eststo  `var'_Overall
		qui areg  `var' ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_*xLnecta_avgmarks* /// 
			if school_size2012 == 1 & post == 1, absorb(district_x_year) cl(necta_districtname)
		eststo  `var'_Smallest
		qui areg  `var' ///
			postxL_rankdist1 postxL_rankdist2 postxL_rankdist9 postxL_rankdist10    ///
			y_*xLnecta_avgmarks* /// 
			if school_size2012 ~= 1 & post == 1, absorb(district_x_year) cl(necta_districtname)
		eststo  `var'_Large	
	}
	esttab necta_avgmarks_Overall necta_avgmarks_Smallest necta_avgmarks_Large necta_passrate_Overall necta_passrate_Smallest necta_passrate_Large ///
		using "$tables/single_diff_small_large.tex", replace ///
		tex label se(3) b(3) alignment(cl) r2 substitute(\_ _ $ \$ ) ///
		keep(postxL_rankdist1 postxL_rankdist2) ///
		collabels(none)	 ///
		mgroups("Average score" "Pass rate" , pattern(1 0 0 1 0 0) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		mtitles("All schools" "Smallest" "Large" "All schools" "Smallest" "Large") ///
		nodepvars nonotes  star(  * 0.1  **  0.05 *** 0.01) 	
